﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using EntLibSample.EntityModel;
using EntLibSample.DAL;
using EntLibSample.DALCommon;

namespace EntLibSample.BLL
{
    public class BLLAddress
    {
        private DBClient dbClient = new DBClient();
        public int AddAddress(Address address)
        {
            try
            {
                dbClient.OpenConnection();
                DALAddress dalAddress = new DALAddress(dbClient.GetCommand());
                int iResult = dalAddress.AddAddress(address);
                dbClient.CloseConnection();
                return iResult;
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
                dbClient.CloseConnection();
                return -1;
            }
        }

        public int UpdateAddress(Address address)
        {
            try
            {
                dbClient.OpenConnection();
                dbClient.BeginTransaction();
                DALAddress dalAddress = new DALAddress(dbClient.GetCommand(), dbClient.transaction);
                int iResult = dalAddress.UpdateAddress(address);
                dbClient.CommitTransaction();
                dbClient.CloseConnection();
                return iResult;
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
                dbClient.RollbackTransaction();
                dbClient.CloseConnection();
                return -1;
            }
        }

        public int DeleteAddress(Address address)
        {
            try
            {
                dbClient.OpenConnection();
                DALAddress dalAddress = new DALAddress(dbClient.GetCommand());
                int iResult = dalAddress.DeleteAddress(address);
                dbClient.CloseConnection();
                return iResult;
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
                dbClient.CloseConnection();
                return -1;
            }
        }

        public Address FindAnAddress(Address address)
        {
            try
            {
                dbClient.OpenConnection();
                DALAddress dalAddress = new DALAddress(dbClient.GetCommand());
                Address oResult = dalAddress.FindAnAddress(address);
                dbClient.CloseConnection();
                return oResult;
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
                dbClient.CloseConnection();
                return null;
            }
        }

        public List<Address> GetAllAddress()
        {
            try
            {
                dbClient.OpenConnection();
                DALAddress dalAddress = new DALAddress(dbClient.GetCommand());
                List<Address> oResult = dalAddress.GetAllAddress();
                dbClient.CloseConnection();
                return oResult;
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
                dbClient.CloseConnection();
                return null;
            }
        }

        public List<Address> GetAddressByCity(Address address)
        {
            try
            {
                dbClient.OpenConnection();
                DALAddress dalAddress = new DALAddress(dbClient.GetCommand());
                List<Address> oResult = dalAddress.GetAddressByCity(address);
                dbClient.CloseConnection();
                return oResult;
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex);
                dbClient.CloseConnection();
                return null;
            }
        }
    }
}
